import request from '@/utils/request'

const setUser = (userObj) => ({ type: 'profile/setUser', payload: userObj })
const setUserProfile = (profileObj) => ({
  type: 'profile/setProfile',
  payload: profileObj,
})

// 发送请求, 获取个人信息的异步action
/**
 * 获取用户自己信息 => 渲染tabbar 个人中心
 * @returns
 */
export const getUser = () => {
  return async (dispatch) => {
    const res = await request.get('/user')
    dispatch(setUser(res.data))
  }
}

/**
 * 获取用户个人资料 => 个人编辑页, 基本渲染
 * @returns
 */
export const getUserProfile = () => {
  return async (dispatch) => {
    const res = await request.get('/user/profile')
    dispatch(setUserProfile(res.data))
  }
}

/**
 * 编辑用户个人资料
 * @param {*} data
 * - name
 * - gender
 * - birthday
 * - intro
 * @returns
 */
export const updateProfile = (data) => {
  return async (dispatch) => {
    await request.patch('/user/profile', data)
    dispatch(getUserProfile())
  }
}

/**
 * 上传用户头像
 * @param {*} formData
 * @returns
 */
export const uploadPhoto = (formData) => {
  return async (dispatch) => {
    await request.patch('/user/photo', formData)
    dispatch(getUserProfile())
  }
}
